home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode New for 1.6 / WorldRayPickSample / Source / WRay_Events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-20  |  6.5 KB  |  412 lines  |  [TEXT/CWIE]

  1. /* 
  2.  *    WRay_Events.c
  3.  *
  4.  *    QuickDraw 3D 1.6 Sample
  5.  *    Robert Dierkes
  6.  *
  7.  *     07/28/98    RDD        Created.
  8.  */
  9.  
  10. /*------------------*/
  11. /*    Include Files    */
  12. /*------------------*/
  13. #include "QD3D.h"
  14.  
  15. #if defined(OS_MACINTOSH) && OS_MACINTOSH
  16. #include <EPPC.h>
  17. #include <DiskInit.h>
  18. #include <ToolUtils.h>
  19. #endif
  20.  
  21. #include "WRay_Error.h"
  22. #include "WRay_Document.h"
  23. #include "WRay_Events.h"
  24. #include "WRay_Main.h"
  25. #include "WRay_Menu.h"
  26. #include "WRay_Pick.h"
  27. #include "WRay_Scene.h"
  28. #include "WRay_System.h"
  29.  
  30.  
  31. /*------------------*/
  32. /*      Constants        */
  33. /*------------------*/
  34.  
  35. /*----------------------*/
  36. /*    Extern Declarations    */
  37. /*----------------------*/
  38. extern    TDocument    gDocument;
  39. extern    TQ3Boolean    gTimeToQuit;
  40.  
  41.  
  42. /*----------------------*/
  43. /*   Type Definitions    */
  44. /*----------------------*/
  45.  
  46.  
  47. /*----------------------*/
  48. /*   Local Prototypes   */
  49. /*----------------------*/
  50. static
  51. void    Events_Activate (
  52.             EventRecord                *pEvent);
  53. static
  54. void    Events_MouseDown (
  55.             EventRecord                *pEvent);
  56. static
  57. void    Events_KeyPress (
  58.             EventRecord                *pEvent);
  59. static
  60. void    Events_Disk (
  61.             EventRecord                *pEvent);
  62. static
  63. void    Events_HandleApp4Event (
  64.             EventRecord                *pEvent);
  65. static
  66. void    Events_ClickInContent(
  67.             EventRecord                *pEvent,
  68.             TDocumentPtr            pDocument);
  69. static
  70. void    Events_GrowBox (
  71.             EventRecord                *pEvent,
  72.             WindowPtr                pWindow);
  73. static
  74. void    Events_ZoomBox (
  75.             EventRecord                *pEvent,
  76.             WindowPtr                pWindow,
  77.             short                    windowPart);
  78.  
  79.  
  80. /*
  81.  *    Events_Initialize
  82.  */
  83. Boolean Events_Initialize(
  84.             void)
  85. {
  86.     FlushEvents(everyEvent, 0);
  87.  
  88.     return true;
  89. }
  90.  
  91.  
  92. /*
  93.  *    Events_Process
  94.  */
  95. void Events_Process(
  96.     void)
  97. {
  98.     EventRecord     event;
  99.     Boolean            doEvent;
  100.  
  101.     while (! gTimeToQuit) {
  102.         /* Is there an event in the queue? */
  103.         doEvent = WaitNextEvent(everyEvent, &event, 0, NULL);
  104.  
  105.         switch (event.what) {
  106.             case nullEvent:
  107.                 if (Pick_Animate(&gDocument) == kQ3Success) {
  108.                     Document_Draw(&gDocument);
  109.                     Scene_Rotate();
  110.                 }
  111.                 else {
  112.                     if (Scene_Rotate() == kQ3Success) {
  113.                         Document_Draw(&gDocument);
  114.                     }
  115.                 }
  116.                 break;
  117.  
  118.             case mouseDown:
  119.                 Events_MouseDown(&event);
  120.                 break;
  121.  
  122.             case keyDown:
  123.             case autoKey:
  124.                 Events_KeyPress(&event);
  125.                 break;
  126.  
  127.             case updateEvt:
  128.                 (void) Events_Update (&event);
  129.                 break;
  130.  
  131.             case activateEvt:
  132.                 Events_Activate (&event);
  133.                 break;
  134.  
  135.             case diskEvt:
  136.                 Events_Disk (&event);
  137.                 break;
  138.  
  139.             case mouseUp:
  140.             case keyUp:
  141.             case osEvt:
  142.             case kHighLevelEvent:
  143.                 break;
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149. #pragma mark -
  150.  
  151. /*
  152.  *    Events_Update
  153.  */
  154. Boolean Events_Update (
  155.             EventRecord *pEvent)
  156. {
  157.     GrafPtr            savedPort;
  158.     WindowPtr        pUpdateWindow;
  159.     Boolean            wasUpdated;
  160.  
  161.     pUpdateWindow = (WindowPtr) pEvent->message;
  162.  
  163.     GetPort (&savedPort);
  164.     SetPort (pUpdateWindow);
  165.  
  166.     if (pUpdateWindow == gDocument.fWindow) {
  167.         BeginUpdate (pUpdateWindow);
  168.         (void) Document_Draw(&gDocument);
  169.         EndUpdate (pUpdateWindow);
  170.  
  171.         wasUpdated = true;
  172.     }
  173.     else {
  174.         /* Unknown window */
  175.         wasUpdated = false;
  176.     }
  177.  
  178.     SetPort (savedPort);
  179.  
  180.     return wasUpdated;
  181. }
  182.  
  183.  
  184. /*
  185.  *    Events_Activate
  186.  */
  187. static
  188. void Events_Activate (
  189.             EventRecord    *pEvent)
  190. {
  191.     WindowPtr        pActiveWindow;
  192.  
  193.     pActiveWindow = (WindowPtr) pEvent->message;
  194.  
  195.     /* Activate window */
  196.     if (pEvent->modifiers & activeFlag)
  197.     {
  198.         /* Enable/Disable items */
  199.         SetPort (pActiveWindow);
  200.     }
  201.     else
  202.     /* Deactivate window */
  203.     {
  204.         /* Enable/Disable items */
  205.     }
  206. }
  207.  
  208.  
  209. /*
  210.  *    Events_MouseDown
  211.  */
  212. static
  213. void Events_MouseDown(
  214.             EventRecord *pEvent)
  215. {
  216.     WindowPtr    pWindow;
  217.     short        windowPart;
  218.  
  219.     windowPart = FindWindow(pEvent->where, &pWindow);
  220.  
  221.     switch (windowPart) {
  222.         case inMenuBar: 
  223.             Menu_Command (MenuSelect (pEvent->where), &gDocument);
  224.             break;
  225.  
  226.         case inSysWindow:
  227.             SystemClick (pEvent, pWindow);
  228.             break;
  229.  
  230.         case inDrag:
  231.             {
  232.                 Rect    bounds;
  233.  
  234.                 bounds = (**GetGrayRgn()).rgnBBox;
  235.  
  236.                 /* Must SetPort before dragging */
  237.                 SetPort (pWindow);
  238.  
  239.                 DragWindow(pWindow, pEvent->where, &bounds);
  240.  
  241.                 bounds = pWindow->portRect;
  242.                 LocalToGlobal((Point *) &bounds.top);
  243.                 LocalToGlobal((Point *) &bounds.bottom);
  244.             }
  245.             break;
  246.  
  247.         case inGrow:
  248.             Events_GrowBox (pEvent, pWindow);
  249.             break;
  250.  
  251.         case inContent:
  252.             if (FrontWindow() != pWindow) {
  253.                 SelectWindow(pWindow);
  254.             }
  255.             SetPort (pWindow);
  256.  
  257.             if (pWindow == NULL) {
  258.                 DEBUG_ASSERT(pWindow != NULL,pWindow)
  259.             }
  260.             else {
  261.                 Events_ClickInContent(pEvent, &gDocument);
  262.             }
  263.         break;
  264.  
  265.         case inGoAway:
  266.             if (TrackGoAway(pWindow, pEvent->where)) {
  267.                 /* Main */
  268.                 if (pWindow == gDocument.fWindow) {
  269.                     gTimeToQuit = kQ3True;
  270.                 }
  271.                 else
  272.                 {
  273.                     /**/
  274.                 }
  275.             }
  276.             break;
  277.  
  278.     case inZoomIn:
  279.     case inZoomOut:
  280.         Events_ZoomBox (pEvent, pWindow, windowPart);
  281.         break;
  282.  
  283.         default:
  284.             break;
  285.     }
  286. }
  287.  
  288.  
  289. /*
  290.  *    Events_KeyPress
  291.  */
  292. static
  293. void Events_KeyPress(
  294.             EventRecord        *pEvent)
  295. {
  296.     char    charCode;
  297.  
  298.     charCode = pEvent->message & charCodeMask;
  299.  
  300.     if (pEvent->modifiers & btnState)
  301.     {
  302.         /* Button is UP with a key */
  303.         if (pEvent->modifiers & cmdKey) {
  304.             Menu_Command (MenuKey (charCode), &gDocument);
  305.         }
  306.         else {
  307.         }
  308.     }
  309.     else {
  310.         /* Button is DOWN with a key */
  311.     }
  312. }
  313.  
  314.  
  315. /*
  316.  *    Events_Disk
  317.  */
  318. static
  319. void Events_Disk(
  320.             EventRecord *pEvent)
  321. {
  322.     Point    topLeft;
  323.  
  324.     SetPt(&topLeft, 100, 100);
  325.     if (HiWord(pEvent->message) != noErr)
  326.         (void) DIBadMount(topLeft, pEvent->message);
  327. }
  328.  
  329.  
  330. #pragma mark -
  331.  
  332. /*
  333.  *    Events_ClickInContent
  334.  */
  335. static
  336. void Events_ClickInContent(
  337.             EventRecord        *pEvent,
  338.             TDocumentPtr    pDocument)
  339. {
  340.     #pragma unused(pEvent)
  341.     #pragma unused(pDocument)
  342. }
  343.  
  344.  
  345. /*
  346.  *    Events_GrowBox
  347.  */
  348. static
  349. void Events_GrowBox (
  350.             EventRecord        *pEvent,
  351.             WindowPtr        pWindow)
  352. {
  353.     long        newSize;
  354.     Rect        newRect,
  355.                 oldRect,
  356.                 screenRect;
  357.     GrafPtr        savedPort;
  358.  
  359.     screenRect = (**GetGrayRgn()).rgnBBox;
  360.     screenRect.left =
  361.     screenRect.top  = 64;
  362.  
  363.     oldRect = newRect = pWindow->portRect;
  364.  
  365.     GetPort (&savedPort);
  366.     SetPort (pWindow);
  367.  
  368.     newSize = GrowWindow (pWindow, pEvent->where, &screenRect);
  369.     if (newSize != 0)
  370.     {
  371.         newRect.right    = newRect.left + (short) newSize;
  372.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  373.         SizeWindow (pWindow,
  374.                     newRect.right  - newRect.left,
  375.                     newRect.bottom - newRect.top,
  376.                     true);
  377.         InvalRect (&oldRect);
  378.         Document_UpdateCameraAspectRatio (&gDocument, &newRect);
  379.     }
  380.  
  381.     SetPort (savedPort);
  382. }
  383.  
  384.  
  385. /*
  386.  *    Events_ZoomBox
  387.  */
  388. static
  389. void Events_ZoomBox (
  390.             EventRecord        *pEvent,
  391.             WindowPtr        pWindow,
  392.             short            windowPart)
  393. {
  394.     Rect    bounds;
  395.     GrafPtr    savedPort;
  396.  
  397.     GetPort (&savedPort);
  398.     SetPort (pWindow);/**/
  399.  
  400.     if (TrackBox (pWindow, pEvent->where, windowPart))
  401.     {
  402.         ZoomWindow (pWindow, windowPart, true);
  403.  
  404.         bounds = pWindow->portRect;
  405.         InvalRect (&bounds);
  406.  
  407.         Document_UpdateCameraAspectRatio (&gDocument, &bounds);
  408.     }
  409.  
  410.     SetPort (savedPort);
  411. }
  412.